package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.hrbxlh.util.ParamConvertUtil;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.RoleDao;
import com.hrbxlh.util.pageUtil;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：RoleActionImpl.java
* Description：角色actionImpl类,负责操作角色数据以及逻辑处理。
* History：
* Date           Author               Desc
* 2017.1.6		  xzx                 建立
* -------------------------------------------------------
*/

@Component("roleActionImpl")
@Transactional
public class RoleActionImpl {
	
	@Resource(name="roleDao")
	private RoleDao roleDao;

	org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(RoleActionImpl.class.getName());
	
	/**
	 * @description 获取全部角色信息
	 * @param Map<String, Object> 如果有查询条件，就将条件set到map中，如果是数据库存在的字段，key值要与column的值相同。
	 * @return List<Map<String,String>> 角色集合
	 * @throws Exception
	 */
	public List<Map<String,Object>> getRoleList(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
		try {
			returnList = roleDao.getRoleList(paramMap);
		} catch (Exception e) {
			logger.error("获取全部角色信息异常getRoleList", e);
			throw e;
		}
		return returnList;
	}
	
	/**
	 * @description 添加角色信息
	 * @param Map<String, Object> key值要与column的值相同。
	 * @return boolean true 成功  false失败
	 * @throws Exception
	 */
	public Map<String, Object> addRole(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> flag = new HashMap<String, Object>();
		try {
//			//1013过滤输入
//			paramMap.put("roleName", ParamConvertUtil.replaceHTMLCodeOne((String)paramMap.get("roleName")));
			Map<String, Object> roleMap = roleDao.getRoleByName(paramMap);
			if(roleMap == null){
				roleDao.addRole(paramMap);
				flag.put("result", true);
			}else{
				flag.put("result", false);
				flag.put("failMsg", "角色名称已存在");
			}
			logger.info("添加角色,参数为："+paramMap.toString());
		} catch (Exception e) {
			logger.error("添加角色信息异常addRole", e);
			throw e;
		}
		return flag;
	}
	
	/**
	 * @description 获取某角色具有的权限信息
	 * @param int roleId：角色id
	 * @return List<Map<String, Object>> 
	 * @throws Exception
	 */
	public List<Map<String, Object>> getPermissionByRoleId(int roleId) throws Exception {

		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();  
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("roleId", roleId);
			resultList = roleDao.getPermissionByRoleId(map);
		} catch (Exception e) {
			logger.error("获取某角色具有的权限信息异常getPermissionByRoleId", e);
			throw e;
		}
		return resultList;
	}
	
	/**
	 * @description 修改角色信息
	 * @param Map<String, Object> 与数据库列值相同
	 * @return boolean true成功　false失败
	 * @throws Exception
	 */
	public Map<String, Object> saveRole(Map<String, Object> paramMap) throws Exception {

		Map<String,Object> flag = new HashMap<String, Object>();
		try {
//			//1013过滤输入
//			paramMap.put("roleName", ParamConvertUtil.replaceHTMLCodeOne((String)paramMap.get("roleName")));
			Map<String, Object> roleMap = roleDao.getRoleByName(paramMap);
			if(roleMap == null){
				roleDao.saveRole(paramMap);
				flag.put("result", true);
			}else{
				flag.put("result", false);
				flag.put("failMsg", "角色名称已存在");
			}
			//List<Integer> list = new ArrayList<Integer>();
			//list.add((Integer)paramMap.get("roleId"));
			//roleDao.delPerission(list);
			//roleDao.addPermission(paramMap);
		} catch (Exception e) {
			logger.error("修改角色异常saveRole", e);
			throw e;
		}
		return flag;
	}
	
	/**
	 * @description 删除角色的方法，传的是角色id。 
	 * @param List<Integer> paramList 角色id集合
	 * @return Map<String, String> result:true成功，false失败，errorMsg：失败信息。
	 * @throws Exception
	 */
	public Map<String, String> delRole(List<String> paramList) throws Exception {
		Map<String, String> resultMap = new HashMap<String, String>(); 
		String failMsg = "";
		boolean resultFlag = false;
		List<Integer> myParamList = new ArrayList<Integer>();
		Map<String, Object> paramMap = null;
		try {
			for(int position=0; position < paramList.size(); position++){
				int roleId = Integer.parseInt(paramList.get(position));
				List<Map<String, Object>> userList = roleDao.getUserByRole(roleId);
				if(userList != null && userList.size()!=0){
					paramMap = new HashMap<String, Object>();
					paramMap.put("roleId", roleId);
					List<Map<String, Object>> roleMessage = roleDao.getRoleList(paramMap);
					failMsg = roleMessage.get(0).get("roleName") + " 角色下有员工隶属，不能删除";
					break;
				}else{
					myParamList.add(roleId);
				}
			}
			
			if(myParamList.size()==paramList.size()){
				roleDao.delRole(myParamList);
				roleDao.delPerission(myParamList);
				resultFlag = true;
			}
			
			resultMap.put("result", String.valueOf(resultFlag));
			resultMap.put("errorMsg", failMsg);
			logger.info("删除角色,参数为："+paramList.toString());
		} catch (Exception e) {
			logger.error("删除角色异常delRole", e);
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 查询角色列表的方法，分页。
	 * @param .Map<String,Object> paramMap pageNow：当前页数，pageSize：每页多少条，其他查询条件与数据库column值一致。
	 * @return Map<String, Object> roleList：角色列表，pageNow：当前页数，totalPage：总共页数。
	 * @throws Exception
	 */
	public Map<String, Object> getRoleListByPage(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			//获取分页数据
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取部门信息
			List<Map<String, Object>> roleList = roleDao.getRoleList(paramMap);
			returnMap.put("roleList", roleList);
			returnMap.put("pageNow", pageNow);
			
			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allRoleList = roleDao.getRoleList(paramMap);
			int totalPage = pageUtil.getTotalPage(allRoleList, pageSize);
			returnMap.put("totalPage", totalPage);	
		} catch (Exception e) {
			logger.error("查询角色列表的方法异常getRoleListByPage", e);
			throw e;
		}
		return returnMap;
	}
}
